﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using System.Threading;
using PServiceBus.Core.Manager;
using PServiceBus.Core.Runtime.Extensions;

namespace PServiceBus.Core.Runtime.Messages {
    /// <summary>
    /// This handler uses the currently loaded object manager
    /// </summary>
    /// <typeparam name="TMessage"></typeparam>
    public class DefaultMessageHandler<TMessage> : MessageHandlerBase<TMessage, TMessage> where TMessage : class, IIdentifiable {

        private IObjectManager<TMessage> _manager;

        public DefaultMessageHandler(string endpoint, int interval, int batchSize) : this(endpoint, null, interval, batchSize) {
        }

        public DefaultMessageHandler(string endpoint, string endpointAlias, int interval, int batchSize) {
            Interval = TimeSpan.FromMilliseconds(interval);
            BatchSize = batchSize;
            _manager = ObjectManager<TMessage>.Create(endpoint, endpointAlias);
            Provider = _manager.Provider;
        }

        public DefaultMessageHandler() { }

        protected override IEnumerable<TMessage> GetMessages(TMessage data) {
            yield return data;
        }
    }
}
